// import wx from 'weixin-js-sdk';
import {Toast} from 'antd-mobile';
import {SERVER_DOMAIN, isLogin, gotoLogin, isWechat,gotoWechat} from './common';
import {get,post} from './request';

export const wxShare = function (url,shareImgUrl) {
    function getShareToken({pageurl}) {
        return get(`/wechat/jstoken`, {pageurl});
    }
    //const memberId_local = localStorage.getItem('memberId');
    //const shareImgUrl = SERVER_DOMAIN + '/dist/assets/img/car.png';
    //const backUrl = window.location.href.split('#').join('_');
    //const url = SERVER_DOMAIN + '/floor/api/startAccess/' + memberId_local + '?backUrl=' + backUrl;
    // if (isWechat()) {
        getShareToken({
            "pageurl": window.location.href.split('#')[0]
        }).then(result => {
            const data = result.ret;
            window.wx.config({
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                appId: data.appid, // 必填，公众号的唯一标识
                timestamp: data.timestamp, // 必填，生成签名的时间戳
                nonceStr: data.noncestr, // 必填，生成签名的随机串
                signature: data.signature,// 必填，签名，
                jsApiList: [
                    "hideAllNonBaseMenuItem",
                    "showMenuItems",
                    // "updateTimelineShareData",
                    // "updateAppMessageShareData",

                    "onMenuShareTimeline", //分享到朋友圈
                    "onMenuShareAppMessage",//分享给好友
                ] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
            });
            window.wx.checkJsApi({
                jsApiList: [
                    "hideAllNonBaseMenuItem",
                    "showMenuItems",
                    // "updateTimelineShareData",
                    // "updateAppMessageShareData",
                    "onMenuShareTimeline", //分享到朋友圈
                    "onMenuShareAppMessage",//分享给好友
                ], // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
                success: function(res) {
                   console.log(res);
                }
            });
            window.wx.ready(function () {
                window.wx.hideAllNonBaseMenuItem(); //隐藏非基础功能按钮
                window.wx.showMenuItems({
                    menuList: ["menuItem:share:appMessage", "menuItem:share:timeline", "menuItem:favorite"] // 要显示的菜单项，所有menu项见附录3
                });
                //分享到朋友圈
                window.wx.onMenuShareTimeline({
                    title: "名食汇", // 分享标题
                    link: url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致   !!!!!!
                    imgUrl: shareImgUrl, // 分享图标
                    success: function () {
                        // 用户确认分享后执行的回调函数
                        Toast.success("分享成功！")
                        //window.location.href = common.SERVER_PATH+'/groupBuy.html#/ginsegOrderList/1';
                    },
                    cancel: function () {
                        // 用户取消分享后执行的回调函数
                        console.log('link====' + this.link);
                        Toast.success("取消分享！")
                        //window.location.href = common.SERVER_PATH+'/groupBuy.html#/ginsegOrderList/1';
                    }
                });
                //分享给好友
                window.wx.onMenuShareAppMessage({
                    title: "名食汇", // 分享标题
                    desc: "亲欢迎光临我的商城~", // 分享描述
                    link: url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                    imgUrl: shareImgUrl, // 分享图标
                    type: '', // 分享类型,music、video或link，不填默认为link
                    dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
                    success: function () {
                        // 用户确认分享后执行的回调函数
                        Toast.success("分享成功！")
                        // console.log(this.title)
                        // console.log(this.desc)
                        console.log(this.link)
                        //window.location.href = common.SERVER_PAlTH+'/groupBuy.html#/ginsegOrderList/1';
                    },
                    cancel: function () {
                        // 用户取消分享后执行的回调函数
                        Toast.success("取消分享！")
                        //window.location.href = common.SERVER_PATH+'/groupBuy.html#/ginsegOrderList/1';
                    }
                });
            });
        });
    // }
}
export const wxInvoice = function () {
    function getShareToken({pageurl}) {
        return get(`/wechat/jstoken`, {pageurl});
    }
    function postInvoice(obj) {
        return post(`/invoice`, obj);
    }
    function getInVoice() {
        return get(`/invoice/basic`);
    }
    //const memberId_local = localStorage.getItem('memberId');
    //const shareImgUrl = SERVER_DOMAIN + '/dist/assets/img/car.png';
    //const backUrl = window.location.href.split('#').join('_');
    //const url = SERVER_DOMAIN + '/floor/api/startAccess/' + memberId_local + '?backUrl=' + backUrl;
    // if (isWechat()) {
    getShareToken({
        "pageurl": encodeURIComponent(window.location.href.split('#')[0])
    }).then(result => {
        const data = result.ret;
        window.wx.config({
            beta: true,
            debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
            appId: data.appid, // 必填，公众号的唯一标识
            timestamp: data.timestamp, // 必填，生成签名的时间戳
            nonceStr: data.noncestr, // 必填，生成签名的随机串
            signature: data.signature,// 必填，签名，
            jsApiList: [
                "chooseInvoiceTitle"
            ] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
        });
        window.wx.checkJsApi({
            jsApiList: [
                "chooseInvoiceTitle"
            ], // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
            success: function(res) {
                console.log(res);
            }
        });
        window.wx.ready(function () {
            window.wx.invoke(' chooseInvoiceTitle ', {
                "scene":"1"
            }, function(res) {
                var obj = {};
                var v = res.choose_invoice_title_info;
                console.log(res)
                if(v.bankName){
                    obj = {
                        state:2,
                        company:v.title,
                        code:v.taxNumber,
                        regAddr:v.companyAddress,
                        regPhone:v.telephone,
                        regBname:v.bankName,
                        regBaccount:v.bankAccount,
                        isDefault:1
                    }
                }else{
                    if(v.taxNumber){
                        obj = {
                            state:1,
                            title:4,
                            company:v.title,
                            code:v.taxNumber,
                            isDefault:1
                        }
                    }else{
                        obj = {
                            state:1,
                            title:3,
                            company:v.title,
                            isDefault:1
                        }
                    }
                }
                postInvoice(obj).then(result=>{
                    if(result.code==200){
                        getInVoice()
                    }
                })
            });
        });
    });
    // }
}
